<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="form.phone" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="form.code" placeholder="请输入验证码"></el-input>

        <el-button
          class="get-code-btn"
          @click="sendCode"
          :disabled="isDisabled"
        >{{ this.isDisabled ? `${this.secondsLeft}秒后重发` : "发送验证码" }}</el-button>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-button type="primary" @click="submitForm">修改密码</el-button>
        <el-button @click="cancelDig">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script>
export default {
  props: ["phoneX"],
  data() {
    return {
      isDisabled: false, // 初始不禁用按钮
      lastClickTime: 0, // 存储最近一次点击时间戳
      secondsLeft: 0, // 倒计时秒数
      form: {
        phone: this.phoneX,
        code: "",
        password: "",
      },
      rules: {
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "密码长度在6到20个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 发送验证码
    async sendCode() {
      // 处理获取验证码
      if (!this.form.phone) {
        return;
      }
      if (this.form.phone !== this.phoneX) {
        return;
      }
      this.countdownClick();
      await this.$axios
        .post("/api/user/sendMsg?phone=" + this.form.phone)
        .then((res) => {
          if (res.data.code == 1) {
            this.$notify({
              title: "获取验证码成功",
              message: res.data.msg,
              type: "success",
            });
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((error) => {
          // 处理请求失败的错误
          this.$message.error(error.response.data.msg);
        });
    },
    // 提交表单
    async submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 验证码验证通过，跳转到下一个页面
          // 在这里提交表单
          await this.$axios
            .post("/api/user/paw", this.form)
            .then((res) => {
              if (res.data.code === 1) {
                this.$message.success(res.data.msg);
                this.$router.push("/loginRegistration");
              } else if (res.data.code === 0) {
                this.$message.error(res.data.msg);
              }
            })
            .catch((error) => {
              // 处理请求失败的错误
              this.$message.error(error.response.data.msg);
            });
        } else {
          this.$message.error("请正确填写信息");
          return false;
        }
      });
    },
    cancelDig() {
      this.$emit("my-event", false, "");
    },
    goToLogin() {
      this.$router.push("/loginRegistration");
    },
    // 倒计时60秒方法
    countdownClick() {
      // 点击触发60秒倒计时
      if (this.isDisabled) {
        return;
      }
      this.isDisabled = true; // 禁用按钮
      this.secondsLeft = 60; // 设置初始倒计时秒数
      this.lastClickTime = Date.now(); // 存储当前点击时间戳
      localStorage.setItem("lastClickTime", this.lastClickTime);
      this.countdown(); // 开始倒计时
    },
    // 开始倒计时
    countdown() {
      if (this.secondsLeft === 0) {
        this.isDisabled = false; // 解除禁用
        return;
      }
      this.secondsLeft--;
      setTimeout(() => {
        this.countdown();
      }, 1000); // 每一秒更新倒计时秒数
    },
    //获取时间戳
    getTimestamp() {
      const lastClickTime = localStorage.getItem("lastClickTime"); // 获取最近一次点击时间戳
      if (lastClickTime !== null && Date.now() - +lastClickTime < 60000) {
        // 如果距离上次点击时间小于60秒
        this.isDisabled = true; // 禁用按钮
        this.secondsLeft = Math.ceil(60 - (Date.now() - +lastClickTime) / 1000);
        this.countdown(); // 开始倒计时
      } else {
        this.isDisabled = false; // 不禁用按钮
        localStorage.removeItem("lastClickTime");
      }
    },
  },
  mounted() {
    this.getTimestamp();
  },
};
</script>
  
  <style scoped>
.get-code-btn {
  float: right;
  margin-top: 5px;
}
</style>